package com.linjiahao.oss.config;

import com.linjiahao.oss.util.StateSecretEntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 随机启动,加载国密配置文件
 * @author suruihuang
 */
@Slf4j
@Component
@Order(1)
public class StateSecretBootRunner implements ApplicationRunner{


    //启用国密标志,1:启用,0:禁用
    private static String ENABLE_STATE_SECRET="1";
    @Override
    public void run(ApplicationArguments args) throws Exception {

        try{
            //加载国密配置文件
            StateSecretSecurityConfig.loadConfigFile();
            log.info("完成加载国密配置文件");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new Exception(e);
        }

        try{
            //缓存国密类信息(方法和成员变量)

            List<String> classList = new ArrayList<String>(StateSecretSecurityConfig.classSet);
            Map<String, Set<String>> classFieldMap= StateSecretSecurityConfig.classFieldMap;

            StateSecretEntityUtil.cacheClassInfo(classList,classFieldMap);

            //释放临时缓存
            StateSecretSecurityConfig.clear();

            log.info("缓存国密类信息(方法和成员变量)成功");
        }catch (Exception e){
            log.error("缓存国密类信息(方法和成员变量)失败",e);
            throw e;
        }

    }

    public static void main(String args[]){
        try {
            new StateSecretBootRunner().run(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
